<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- 创建时间范围选择 -->
      <el-form-item label="接收时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 220px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="接收来源" prop="receiveSource">
        <el-select
          v-model="queryParams.receiveSource"
          placeholder="请选择接收来源"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_receive_source"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客服类型" prop="serviceType">
        <el-select
          v-model="queryParams.serviceType"
          placeholder="请选择客服类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_service_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题小类" prop="subissueType">
        <el-select
          v-model="queryParams.subissueType"
          placeholder="请选择问题小类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_subissuetype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="OA单号" prop="oaOrderid">
        <el-input
          v-model="queryParams.oaOrderid"
          placeholder="请输入OA工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="线路" prop="line">
        <el-input
          v-model="queryParams.line"
          placeholder="请输入线路"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号码" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驾驶员" prop="driver">
        <el-input
          v-model="queryParams.driver"
          placeholder="请输入驾驶员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属机构" prop="organization">
        <el-select
          v-model="queryParams.organization"
          placeholder="请选择所属机构"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_organization"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="具体站点" prop="station">
        <el-input
          v-model="queryParams.station"
          placeholder="请输入具体站点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="具体内容" prop="details">
        <el-input
          v-model="queryParams.details"
          placeholder="请输入具体内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市民姓名" prop="citizenname">
        <el-input
          v-model="queryParams.citizenname"
          placeholder="请输入市民姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['visiting:visiting:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['visiting:visiting:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['visiting:visiting:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['visiting:visiting:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="visitingList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="receiveId" />
      <el-table-column
        label="接收时间"
        align="center"
        prop="receiveTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.receiveTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收来源" align="center" prop="receiveSource">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_receive_source"
            :value="scope.row.receiveSource"
          />
        </template>
      </el-table-column>
      <el-table-column label="客服类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_service_type"
            :value="scope.row.serviceType"
          />
        </template>
      </el-table-column>
      <el-table-column label="问题小类" align="center" prop="subissueType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_subissuetype"
            :value="scope.row.subissueType"
          />
        </template>
      </el-table-column>
      <el-table-column label="OA工单号" align="center" prop="oaOrderid" />
      <el-table-column label="线路" align="center" prop="line" />
      <el-table-column label="车牌号码" align="center" prop="plateNumber" />
      <el-table-column label="驾驶员" align="center" prop="driver" />
      <el-table-column label="所属机构" align="center" prop="organization">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_organization"
            :value="scope.row.organization"
          />
        </template>
      </el-table-column>
      <el-table-column label="具体站点" align="center" prop="station" />
      <el-table-column label="具体内容" align="center" prop="details" />
      <el-table-column label="市民姓名" align="center" prop="citizenname" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber" />
      <el-table-column label="回复内容" align="center">
        <template slot-scope="scope">
          {{ stripTags(scope.row.replyContent) }}
        </template>
      </el-table-column>
      <el-table-column
        label="录音附件"
        align="center"
        prop="recordingAttachment"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['visiting:visiting:edit']"
            style="margin: 0 0 5px 10px"
            >修改</el-button
          >
          <!-- <el-button
            size="small"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['visiting:visiting:edit']"
            >修改</el-button
          > -->
          <el-button
            size="small"
            type="info"
            @click="handleDetail(scope.row)"
            v-hasPermi="['visiting:visiting:detail']"
            style="margin-bottom: 5px"
            >详情</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['visiting:visiting:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情页对话框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="1200px"
      @close="closeDialog"
    >
      <el-descriptions title="来访详情" direction="vertical" :column="4" border>
        <el-descriptions-item label="编号" prop="receIveId">{{
          visitingDetail.receiveId
        }}</el-descriptions-item>

        <el-descriptions-item label="接收时间" prop="receiveTime">{{
          visitingDetail.receiveTime
        }}</el-descriptions-item>
        <el-descriptions-item label="接收来源">
          <dict-tag
            :options="dict.type.sys_receive_source"
            :value="visitingDetail.receiveSource"
          />
        </el-descriptions-item>
        <el-descriptions-item label="客服类型">
          <dict-tag
            :options="dict.type.sys_service_type"
            :value="visitingDetail.serviceType"
          />
        </el-descriptions-item>
        <el-descriptions-item label="问题小类">
          <dict-tag
            :options="dict.type.sys_subissuetype"
            :value="visitingDetail.subissueType"
          />
        </el-descriptions-item>
        <el-descriptions-item label="OA工单号">
          {{
            visitingDetail.oaOrderid == null ? "无" : visitingDetail.oaOrderid
          }}
        </el-descriptions-item>
        <el-descriptions-item label="线路">
          {{ visitingDetail.line == null ? "无" : visitingDetail.line }}
        </el-descriptions-item>
        <el-descriptions-item label="车牌号码">
          {{
            visitingDetail.plateNumber == null
              ? "无"
              : visitingDetail.plateNumber
          }}
        </el-descriptions-item>
        <el-descriptions-item label="驾驶员">
          {{ visitingDetail.driver == null ? "无" : visitingDetail.driver }}
        </el-descriptions-item>
        <el-descriptions-item label="所属机构">
          <dict-tag
            :options="dict.type.sys_organization"
            :value="visitingDetail.organization"
          />
        </el-descriptions-item>
        <el-descriptions-item label="具体站点">
          {{ visitingDetail.station == null ? "无" : visitingDetail.station }}
        </el-descriptions-item>
        <el-descriptions-item label="终点站">
          {{
            visitingDetail.destination == null
              ? "无"
              : visitingDetail.destination
          }}
        </el-descriptions-item>
        <el-descriptions-item label="具体内容">
          {{ visitingDetail.details == null ? "无" : visitingDetail.details }}
        </el-descriptions-item>
        <el-descriptions-item label="来访人" prop="visitorName">{{
          visitingDetail.citizenname
        }}</el-descriptions-item>
        <el-descriptions-item label="来访人电话" prop="visitorPhone"
          >{{ visitingDetail.phoneNumber }}
        </el-descriptions-item>
        <el-descriptions-item label="回复内容">
          {{
            visitingDetail.replyContent == null
              ? "无"
              : stripTags(visitingDetail.replyContent)
          }}
        </el-descriptions-item>
        <el-descriptions-item label="录音附件">
          <audio ref="audio" class="aud" controls :src="'http://localhost/dev-api/' + visitingDetail.recordingAttachment" :disabled="(visitingDetail.recordingAttachment == null ) ? true : false" >
          </audio>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!-- 添加或修改来访记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="接收时间" prop="receiveTime">
          <el-date-picker
            clearable
            v-model="form.receiveTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="请选择接收时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="接收来源" prop="receiveSource">
          <el-radio-group v-model="form.receiveSource">
            <el-radio
              v-for="dict in dict.type.sys_receive_source"
              :key="dict.value"
              :label="dict.value"
              :disabled="updateDisabled"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="客服类型" prop="serviceType">
          <el-select v-model="form.serviceType" placeholder="请选择客服类型">
            <el-option
              v-for="dict in dict.type.sys_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题小类" prop="subissueType">
          <el-select v-model="form.subissueType" placeholder="请选择问题小类">
            <el-option
              v-for="dict in dict.type.sys_subissuetype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="OA工单号" prop="oaOrderid">
          <el-input v-model="form.oaOrderid" placeholder="请输入OA工单号" />
        </el-form-item>
        <el-form-item label="线路" prop="line">
          <el-input v-model="form.line" placeholder="请输入线路" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="plateNumber">
          <el-input v-model="form.plateNumber" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="驾驶员" prop="driver">
          <el-input v-model="form.driver" placeholder="请输入驾驶员" />
        </el-form-item>
        <el-form-item label="所属机构" prop="organization">
          <el-select v-model="form.organization" placeholder="请选择所属机构">
            <el-option
              v-for="dict in dict.type.sys_organization"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="具体站点" prop="station">
          <el-input v-model="form.station" placeholder="请输入具体站点" />
        </el-form-item>
        <el-form-item label="终点站" prop="destination">
          <el-input v-model="form.destination" placeholder="请输入终点站" />
        </el-form-item>
        <el-form-item label="具体内容" prop="details">
          <el-input v-model="form.details" placeholder="请输入具体内容" />
        </el-form-item>
        <el-form-item label="市民姓名" prop="citizenname">
          <el-input v-model="form.citizenname" placeholder="请输入市民姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="回复内容">
          <editor v-model="form.replyContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="录音附件" prop="recordingAttachment" multiple=false>
          <file-upload v-model="form.recordingAttachment"  :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVisiting,
  getVisiting,
  delVisiting,
  addVisiting,
  updateVisiting,
} from "@/api/visiting/visiting";

export default {
  name: "Visiting",
  dicts: [
    "sys_service_type",
    "sys_receive_source",
    "sys_subissuetype",
    "sys_organization",
  ],
  data() {
    return {
      audio: new Audio(""),
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 修改禁用
      updateDisabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 来访记录表格数据
      visitingList: [],
      // 单条来访详情
      visitingDetail: {},
      //mp3文件
      recordingAttachment: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiveTime: null,
        receiveSource: null,
        serviceType: null,
        subissueType: null,
        oaOrderid: null,
        line: null,
        plateNumber: null,
        driver: null,
        organization: null,
        station: null,
        details: null,
        citizenname: null,
        phoneNumber: null,
        selectedDateRange: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiveTime: [
          { required: true, message: "接收时间不能为空", trigger: "blur" },
        ],
        receiveSource: [
          { required: true, message: "接收来源不能为空", trigger: "change" },
        ],
        serviceType: [
          { required: true, message: "客服类型不能为空", trigger: "change" },
        ],
        subissueType: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        receiveTime: [
          { required: true, message: "接收时间不能为空", trigger: "blur" },
        ],
        receiveSource: [
          { required: true, message: "接收来源不能为空", trigger: "change" },
        ],
        serviceType: [
          { required: true, message: "客服类型不能为空", trigger: "change" },
        ],
        subissueType: [
          { required: true, message: "问题小类不能为空", trigger: "change" },
        ],
        details: [
          { required: true, message: "具体内容不能为空", trigger: "blur" },
        ],
        citizenname: [
          { required: true, message: "市民姓名不能为空", trigger: "blur" },
        ],
        phoneNumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
        ],
        replyContent: [
          { required: true, message: "回复内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    // this.getDicts("sys_receive_source").then((response) => {
    //   this.receiveSourceOptions = response.data;
    //   console.log(this.receiveSourceOptions);
    // });
  },
  methods: {
    /** 查询来访记录列表 */
    getList() {
      this.loading = true;
      listVisiting(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.visitingList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    stripTags(html) {
      return html.replace(/<\/?[^>]+(>|$)/g, "");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        receiveId: null,
        receiveTime: null,
        receiveSource: null,
        serviceType: null,
        subissueType: null,
        oaOrderid: null,
        line: null,
        plateNumber: null,
        driver: null,
        organization: null,
        station: null,
        destination: null,
        details: null,
        citizenname: null,
        phoneNumber: null,
        replyContent: null,
        recordingAttachment: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      // console.log(typeof(this.dateRange));
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.dateRange = null;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.receiveId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.receiveId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.updateDisabled = false;

      this.title = "添加来访记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const receiveId = row.receiveId || this.ids;
      getVisiting(receiveId).then((response) => {
        this.form = response.data;
        console.log(this.form);
        this.open = true;
        this.updateDisabled = true;
        console.log(this.updateDisabled);
        this.title = "修改来访记录";
      });
    },


    closeDialog() {
      this.dialogVisible = false;
      this.recordingAttachment = null;
      // 重新加载音频文件
      // var audio=this.$refs.audio
      // audio.src = this.visitingDetail.recordingAttachment
      // console.log(audio);
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      const receiveId = row.receiveId;
      getVisiting(receiveId).then((response) => {
        this.visitingDetail = response.data;
        console.log(this.visitingDetail);
        this.dialogVisible = true;
        this.title = "来访详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.receiveId != null) {
            updateVisiting(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVisiting(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const receiveIds = row.receiveId || this.ids;
      this.$modal
        .confirm('是否确认删除来访记录编号为"' + receiveIds + '"的数据项？')
        .then(function () {
          return delVisiting(receiveIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "visiting/visiting/export",
        {
          ...this.queryParams,
        },
        `visiting_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
</style>